package com.eastfair.venueservice.service.impl;

import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.api.VenueReportFeign;
import com.eastfair.home.dto.VenueReportDTO;
import com.eastfair.venueservice.dto.ExhibitionManageDTO;
import com.eastfair.venueservice.dto.ExhibitionServiceProviderDTO;
import com.eastfair.venueservice.dto.ServiceProviderSpaceDTO;
import com.eastfair.venueservice.enumeration.ServiceProviderTypeEnum;
import com.eastfair.venueservice.service.VenueManageVenueReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 主场报馆 service.
 *
 * @author dqq
 * @date 2022 /11/2
 */
@Service
@Slf4j
public class VenueManageVenueReportServiceImpl implements VenueManageVenueReportService {

    @Resource
    private VenueReportFeign venueReportFeign;

    @Override
    public boolean saveBatch(List<VenueReportDTO> list) {
        log.info("saveBatch - 批量保存主场报馆记录, list={}", list);
        R<Boolean> result = venueReportFeign.saveBatch(list);
        log.info("saveBatch - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean saveBatchByExhibitionManage(ExhibitionManageDTO exhibitionDTO) {
        List<ExhibitionServiceProviderDTO> exhibitionServiceProviderList = exhibitionDTO.getExhibitionServiceProviderList();
        if (exhibitionServiceProviderList == null || exhibitionServiceProviderList.isEmpty()) {
            return false;
        }
        // 展会服务商关系列表+服务展厅关系列表 转换为 主场报馆列表
        List<VenueReportDTO> venueReportList = new ArrayList<>(16);
        Long exhibitionId = exhibitionDTO.getId();
        String exhibitionName = exhibitionDTO.getExhibitionName();
        for (ExhibitionServiceProviderDTO exhibitionServiceProviderDTO : exhibitionServiceProviderList) {
            if (!ServiceProviderTypeEnum.HOME_SERVICE_PROVIDER.getCode().equals(exhibitionServiceProviderDTO.getServiceProviderType())) {
                continue;
            }
            List<ServiceProviderSpaceDTO> serviceProviderSpaceDTOList = exhibitionServiceProviderDTO.getServiceProviderSpaceList();
            if (serviceProviderSpaceDTOList == null || serviceProviderSpaceDTOList.isEmpty()) {
                continue;
            }
            Long serviceProviderId = exhibitionServiceProviderDTO.getServiceProviderId();
            String serviceProviderName = exhibitionServiceProviderDTO.getServiceProviderName();
            for (ServiceProviderSpaceDTO serviceProviderSpaceDTO : serviceProviderSpaceDTOList) {
                VenueReportDTO venueReportDTO = new VenueReportDTO();
                venueReportDTO.setExhibitionManageId(exhibitionId);
                venueReportDTO.setExhibitionManageName(exhibitionName);
                venueReportDTO.setHomeId(serviceProviderId);
                venueReportDTO.setHomeName(serviceProviderName);
                venueReportDTO.setSpaceCode(serviceProviderSpaceDTO.getSpaceCode());
                venueReportDTO.setSpaceName(serviceProviderSpaceDTO.getSpaceName());
                venueReportList.add(venueReportDTO);
            }
        }
        // 批量保存主场报馆记录
        return saveBatch(venueReportList);
    }
}